package com.aimusic.controller;

import com.aimusic.domain.Result;
import com.aimusic.domain.dto.LoginRequestDTO;
import com.aimusic.domain.dto.RegisterDTO;
import com.aimusic.domain.vo.LoginResponseVO;
import com.aimusic.service.IUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
public class LoginController {
    private final IUserService userService;

    /**
     * 登录
     * @param loginRequestDTO
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody LoginRequestDTO loginRequestDTO) {
        LoginResponseVO vo = userService.login(loginRequestDTO);
        if (vo != null) {
            return Result.success(vo);
        }
        return Result.error("用户名或密码错误");
    }

    /**
     * 注册账号发送验证码
     * @param loginRequestDTO
     * @return
     */
    @PostMapping("/send-sms")
    public Result sendSms(@RequestBody LoginRequestDTO loginRequestDTO) {
        userService.sendSms(loginRequestDTO,1);
        return Result.success(true);
    }

    /**
     * 找回密码发送验证码
     * @param loginRequestDTO
     * @return
     */
    @PostMapping("/send-forgot-sms")
    public Result sendForgotSms(@RequestBody LoginRequestDTO loginRequestDTO) {
        userService.sendSms(loginRequestDTO,2);
        return Result.success(true);
    }

    /**
     * 发送验证码并存入Redis
     * @param registerDTO
     * @return
     */
    @PostMapping("/register")
    public Result register(@RequestBody RegisterDTO registerDTO) {
        userService.register(registerDTO);
        return Result.success(true);
    }

    /**
     * 重置密码
     * @param registerDTO
     * @return
     */
    @PostMapping("/forgot-password")
    public Result forgetPassword(@RequestBody RegisterDTO registerDTO) {
        userService.forgetPassword(registerDTO);
        return Result.success(true);
    }
}
